home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / common / sources.c / winfileutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-22  |  7.9 KB  |  330 lines

  1. /*
  2.     File: WinFileUtils.c
  3.  
  4.     Copyright (c) 1996, Adobe Systems Incorporated.
  5.     All rights reserved.
  6.  
  7.     C source file for file utilities.
  8. */
  9.  
  10. #include "WinFileUtils.h"
  11.  
  12. /*****************************************************************************/
  13.  
  14. Boolean PISetSaveDirectory (Handle alias, Str255 s, 
  15.                                   Boolean query, short *rVRefNum)
  16. {
  17.  
  18. #if 0
  19.     CInfoPBRec            pb;
  20.     Boolean                wasChanged;
  21.     FSSpec                spec;
  22.     int32                gotErr = 0;
  23.     
  24.     if (alias != NULL)
  25.     {
  26.     
  27.         ResolveAlias(nil, (AliasHandle) alias, &spec, &wasChanged);
  28.         
  29.         /* set up file param structure so we can get whether we've been handed
  30.            a file or a directory */
  31.         
  32.         pb.dirInfo.ioCompletion = nil;
  33.         pb.dirInfo.ioNamePtr = spec.name;
  34.         pb.dirInfo.ioVRefNum = spec.vRefNum;
  35.         pb.dirInfo.ioFRefNum = 0;
  36.         pb.dirInfo.ioFDirIndex = 0;
  37.         pb.dirInfo.ioDrDirID = spec.parID;
  38.         
  39.         gotErr = PBGetCatInfoSync( &pb );
  40.         
  41.         /* now check for NO error and this is NOT a directory */
  42.         // nah, don't need to check for error.
  43.         /* even if we got an error, these fields will be valid (enough) */
  44.         
  45.         if (spec.name[0] > 0)
  46.         /* the scripting system passed us something.  Figure out what to do with it. */
  47.         {
  48.             if (pb.dirInfo.ioFlAttrib & ioDirMask)
  49.             { // the scripting system passed us a directory.  Add filename to it
  50.                // if we're not popping a dialog.
  51.                                     
  52.                 if (!query)
  53.                 { // if we were going to show this, we could not
  54.                   // make it :dir:name since that would be bad for the file dialog
  55.                     spec.name[++spec.name[0]] = ':';
  56.                     PICopy(&spec.name[ (spec.name[0]+1) ], &s[1], s[0]);
  57.                     spec.name[0] += s[0];
  58.                     s[0] = spec.name[0]+1;
  59.                     s[1] = ':'; // since we're a directory
  60.                     PICopy (&s[2], &spec.name[1], spec.name[0]); // copy name to output string
  61.                 }
  62.                 else
  63.                 { // querying so set dir
  64.                     LMSetSFSaveDisk(-pb.dirInfo.ioVRefNum);
  65.                     LMSetCurDirStore(pb.dirInfo.ioDrDirID);
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 /* we were given a full pathname. Copy the name. */
  71.                 PICopy (s, spec.name, (spec.name[0])+1);
  72.                 
  73.                 if (query)
  74.                 { // we're showing our dialog, set set directory from alias
  75.                     LMSetSFSaveDisk(-spec.vRefNum);
  76.                     LMSetCurDirStore(spec.parID);
  77.                 }
  78.             }
  79.             /* otherwise we'll just use whatever the export parameter block
  80.                has passed us */
  81.         }
  82.  
  83.         /* set gStuff->vRefNum as working directory (alias) from vRefNum
  84.            from hard vRefNum/parID */
  85.         PICloseAndOpenWD(spec.vRefNum, spec.parID, rVRefNum);
  86.         return TRUE;
  87.     }
  88.     else return FALSE; // aliashandle was null
  89. #endif
  90.     return FALSE;
  91. }
  92.  
  93. /*****************************************************************************/
  94.  
  95. Boolean PICreateFile (Str255 filename, short vRefNum, const ResType creator, 
  96.                       const ResType type, FileHandle *fRefNum, short *result)
  97. {
  98. #if 0
  99.     /* Mark the file as not being open. */
  100.     
  101.     *fRefNum = 0;
  102.     
  103.     /* Create the file. */
  104.  
  105.     (void) FSDelete (filename, vRefNum);
  106.  
  107.     if (!TestAndStoreResult (result, Create (filename, vRefNum, creator, type)))
  108.         return FALSE;
  109.         
  110.     /* Open the file. */
  111.  
  112.     if (!TestAndStoreResult (result, FSOpen (filename, vRefNum, fRefNum)))
  113.     {
  114.         /* failure */
  115.         (void) FSDelete (filename, vRefNum);
  116.         return FALSE;
  117.     }
  118.     else
  119.         return TRUE; // succeeded
  120. #endif
  121.     return FALSE; //failed
  122. }
  123.  
  124. /*****************************************************************************/
  125.  
  126. Boolean PICloseFile (Str255 filename, short vRefNum,
  127.                      FileHandle fRefNum, Boolean sameNames,
  128.                      Boolean *dirty, AliasHandle *alias, short *result)
  129. {
  130.  
  131.     OSErr            gotErr = noErr;
  132. #if 0
  133.     FSSpec            spec;
  134.     
  135.     // Close the file if it is open.
  136.     if (fRefNum != 0)
  137.         (void) TestAndStoreResult (result, FSClose (fRefNum));
  138.         
  139.     // Delete the file if we had an error.    
  140.     if (*result != noErr)
  141.         (void) FSDelete (filename, vRefNum);
  142.             
  143.     // Flush the volume anyway.
  144.     if (!TestAndStoreResult (result, FlushVol (NULL, vRefNum))) return FALSE;
  145.     
  146.     // Mark the file as clean.
  147.     *dirty = FALSE;
  148.     
  149.     /* create alias for scripting system */
  150.     
  151.     /* first, make FSSpec record */
  152.     FSMakeFSSpec(vRefNum, 0, filename, &spec);
  153.  
  154.     if (sameNames)
  155.     { // didn't enter new filename, so set filename to nothing
  156.         spec.name[ (spec.name[0] = 0)+1 ] = 0;
  157.     } // otherwise use filename and store entire reference
  158.  
  159.     gotErr = NewAlias(nil, &spec, alias);
  160. #endif
  161.     return (gotErr == noErr);
  162.     
  163. }
  164.     
  165. /*****************************************************************************/
  166.  
  167. Boolean TestAndStoreResult (short *res, OSErr result)
  168. {
  169.     if (result != noErr)
  170.         {
  171.         if (*res == noErr)
  172.             *res = result;
  173.         return FALSE;
  174.         }
  175.     else
  176.         return TRUE;    
  177. }
  178.     
  179. /*****************************************************************************/
  180.  
  181. Boolean TestAndStoreCancel (short *res, Boolean tocancel)
  182. {    
  183.     if (tocancel)
  184.         {
  185.         if (*res == noErr)
  186.             *res = userCanceledErr;
  187.         return FALSE;
  188.         }
  189.     else
  190.         return TRUE;    
  191. }
  192.  
  193. /*****************************************************************************/
  194. // Creates new working directory and deletes old one if it was valid
  195.  
  196. void PICloseAndOpenWD(const short vRefNum, const long dirID, short *rVRefNum)
  197. {
  198.     short oldVRefNum = *rVRefNum;
  199. #if 0    
  200.     OpenWD(vRefNum, dirID, 0, rVRefNum);
  201.     if (*rVRefNum != oldVRefNum && oldVRefNum < -100)
  202.     { // small negative numbers are actual volumes (-1, etc.)
  203.       // large negative numbers (-32766, etc.) are WD's to be closed
  204.         CloseWD(oldVRefNum); // close our old vRefNum
  205.     }
  206. #endif
  207. }
  208.  
  209. #if 0 /* I haven't had a chance to fix these up and test them
  210.        * so that's why they're commented out.
  211.        */
  212. /************************* File I/O Function ****************************/
  213. /* Normal Order */
  214.  
  215. long    eofPos = -1L;
  216.  
  217. OSErr SetFPos (int fHand, short posMode, long posOff)
  218. {
  219.     long    newPos;
  220.  
  221.  
  222.     if (eofPos < 0) {   /* Initialize eofPos */
  223.         newPos = _llseek(fHand, 0, fsFromMark);
  224.         eofPos = _llseek(fHand, 0, fsFromEnd);
  225.         if (posMode == fsFromMark)
  226.             newPos = _llseek(fHand, newPos, fsFromStart);
  227.     }
  228.  
  229.     newPos = _llseek(fHand, posOff, posMode);
  230.     if (newPos == -1L || newPos >= eofPos)
  231.         return eofErr;
  232.     else
  233.         return noErr;
  234. }
  235.  
  236. OSErr GetFPos (int fHand, long *filePos)
  237. {
  238.     if ((*f = _llseek(fHand, 0, fsFromMark)) == -1L)
  239.         return -1;
  240.     else
  241.         return noErr;
  242. }
  243.  
  244.  
  245. OSErr GetEOF (int fHand,long *logEOF)
  246. {
  247.     if ((*logEOF = _llseek(fHand, 0, fsFromEnd)) == -1L)
  248.         return -1;
  249.     else
  250.         return noErr;
  251. }
  252.  
  253.  
  254. unsigned8 readBYTE(GPtr data)
  255. {
  256.     int       fHand = data->gStuff->dataFork;
  257.     unsigned8   cdata;
  258.  
  259.     if (_lread(fHand, (LPSTR)&cdata, sizeof(unsigned8)) != sizeof(unsigned8))
  260.         data->gResult = readErr;
  261.  
  262.     return (cdata);
  263. }
  264.  
  265.  
  266. unsigned16 readWORD(GPtr data)
  267. {
  268.     int       fHand = data->gStuff->dataFork;
  269.     unsigned16  cdata;
  270.  
  271.     if (_lread(fHand, (LPSTR)&cdata, sizeof(unsigned16)) != sizeof(unsigned16))
  272.         data->gResult = readErr;
  273.  
  274.     return (cdata);
  275. }
  276.  
  277.  
  278. unsigned32 readDWORD(GPtr data)
  279. {
  280.     int       fHand = data->gStuff->dataFork;
  281.     unsigned16  cdata;
  282.  
  283.     if (_lread(fHand, (LPSTR)&cdata, sizeof(unsigned32)) != sizeof(unsigned32))
  284.         data->gResult = readErr;
  285.  
  286.     return (cdata);
  287. }
  288.  
  289.  
  290. void readCount(GPtr data, Ptr buf, unsigned32 count)
  291. {
  292.     int       fHand = data->gStuff->dataFork;
  293.  
  294.     if (_lread(file, (LPSTR)buf, (short)count) != count)
  295.         data->gResult = readErr;
  296. }
  297.  
  298. void writeBYTE(GPtr data, BYTE cdata)
  299. {
  300.     short       file = (short) data->gStuff->dataFork;
  301.  
  302.     if (_lwrite(file, (LPSTR)&cdata, sizeof(BYTE)) != sizeof(BYTE))
  303.         data->gResult = writErr;
  304. }
  305.  
  306. void writeWORD(GPtr data, WORD cdata)
  307. {
  308.     short       file = (short) data->gStuff->dataFork;
  309.  
  310.     if (_lwrite(file, (LPSTR)&cdata, sizeof(WORD)) != sizeof(WORD))
  311.         data->gResult = writErr;
  312. }
  313.  
  314. void writeDWORD(GPtr data, DWORD cdata)
  315. {
  316.     short       file = (short) data->gStuff->dataFork;
  317.  
  318.     if (_lwrite(file, (LPSTR)&cdata, sizeof(DWORD)) != sizeof(DWORD))
  319.         data->gResult = writErr;
  320. }
  321.  
  322. void writeCount(GPtr data, Ptr buf, unsigned32 count)
  323. {
  324.     short       file = (short) data->gStuff->dataFork;
  325.  
  326.     if (_lwrite(file, (LPSTR)buf, (short)count) != count)
  327.         data->gResult = writErr;
  328. }
  329.  
  330. #endif